<?php
/**
 * Created by PhpStorm.
 * User: 虚空之翼
 * Date: 2017/5/8
 * Time: 下午2:53
 */

namespace app\index\controller;


use think\Config;
use think\Controller;
use think\Loader;
use wechat\TPWechat;
use app\index\model\WechatUser as WechatUserModel;
use app\admin\model\Config as ConfigModel;
use Hooklife\ThinkphpWechat\Wechat as WechatServer;
use think\Session;


class Base extends Controller
{

    protected function _initialize()
    {
        session('openid', 'oSaaIwuf1OXIgwxnv9uN31nBKw6o');//测试
        //      session('thirdUserId', '1001');

        /* 读取数据库中的配置 */
        $config = cache('db_config_data');
        if (!$config) {
            $configModel = new ConfigModel();
            $config = $configModel->lists();
            cache('db_config_data', $config);
        }
        config($config); //添加配置
        //存域名
        session('requestUri', 'http://' .  $_SERVER["SERVER_NAME"]  . $_SERVER["REQUEST_URI"]);
        $openid = session('openid');
        //授权
        if (empty($openid)) {
            $oauth = WechatServer::app()->oauth;

            return $oauth->redirect()->send();
        }
    }
    /**
     * 获取企业号签名
     */
    public function jssdk()
    {
        Loader::import('wechat\TPWechat', EXTEND_PATH);
        $weObj = new TPWechat(config('wechat'));
        $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $jsSign = $weObj->getJsSign($url);
        $this->assign("jsSign", $jsSign);
    }

}